﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Newtonsoft.Json;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Forum
{
    public partial class UserInfo : PortalPage
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
                return;
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
                return;
            }
            if (!IsPostBack)
            {
                CheckLogin(true);
                LoadData();
            }
        }

        public void LoadData()
        {
            PartyForum.RunForumUserInfoStatistics(CurrentUser.LoginID);
            this.txtUserName.Text = CurrentUser.UserName;
            this.lbBranchName.Text = ControlBusiness.GetBranchPickerText(CurrentUser.BranchID);
            this.lbSex.Text = CurrentUser.Gender == "0" ? "男" : CurrentUser.Gender == "1" ? "女" : "未知";
            
            var users = DB.PartyForumUserInfos.Where(a => a.LoginID == CurrentUser.LoginID);

            if (users.Any())
            {
                var user = users.ToList()[0];
                imgPicture.Attributes["src"] = UpFileBusiness.GetFileUrl(user.ImageID);
                this.lbScore.Text = user.Score.ToString();
                lbBestNum.Text = user.EssenceNum.ToString();
                lbPostNum.Text = user.PostNum.ToString();
                lbReplayNum.Text = user.ReplyCount.ToString();
            }
            else
            {
                DB.PartyForumUserInfos.Add(new PartyForumUserInfo()
                {
                    ID = StringHelper.NewID,
                    BrowseNum = 0,
                    EssenceNum = 0,
                    ImageID = "",
                    Level = 0,
                    LoginID = CurrentUser.LoginID,
                    NickName = "",
                    PostNum = 0,
                    ReplyCount = 0,
                    Score = 0
                });
                DB.SaveChanges();
            }

        }

        #region 
        private void WriteList()
        {
            DataTable dt = PartyObjectScore.GetGridDataMemberForum(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize),CurrentUser.LoginID).Tables[0];


            dt.Columns.Add("ScoreDate2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["ScoreDate2"] = Convert.ToDateTime(dr["ScoreDate"]).ToString("yyyy-MM-dd HH:mm");
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyObjectScore.GetGridDataMemberForum(0, 1,CurrentUser.LoginID).Tables[1].Rows[0][0].ToString());
            Response.End();
        }
        #endregion

        #region 提交约见

        public bool CheckValue()
        {

            var rs = true;


            if (this.hdImageId.Text.Trim() == "")
            {
                FineUI.Alert.Show("请上传图片");
                rs = false;
            }
            return rs;

        }


        public void SaveData()
        {
            if (!CheckValue())
            {
                return;
            }
            var user = DB.PartyForumUserInfos.First(a => a.LoginID == CurrentUser.LoginID);
            user.ImageID = this.hdImageId.Text;
            if (DB.SaveChanges() > 0)
            {
                FineUI.Alert.Show("保存成功，请稍后再试。");
            }
            else
            {
                FineUI.Alert.Show("提交失败，请稍后再试。");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        #endregion

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID, "ForumUserImage", SysEnum.UploadFileType.uploadimage);
            if (rs.IsSuccess)
            {
                this.hdImageId.Text = rs.FileID;
                FineUI.Alert.Show("上传成功！", String.Empty);
            }
            else
            {
                FineUI.Alert.Show("上传失败！" + rs.Message, String.Empty);
            }
        }
    }
}